package com.algo.generator.common;

import com.algo.generator.constant.ResponseCodeEnum;

import java.io.Serializable;

/**
 * 通用返回对象
 * @param <T>
 */
public class BaseResponse<T> implements Serializable {
    private static final long serialVersionUID = -6168385396584895333L;

    // 状态码: 0:成功,-1:失败
    private Integer code;

    // 状态信息
    private String message;

    // 数据
    private T data;

    public BaseResponse() {
    }

    public BaseResponse(ICodeMSG codeEnum) {
        this.code = codeEnum.getCode();
        this.message = codeEnum.getMessage();
        this.data = null;
    }

    public BaseResponse(Integer code, String message) {
        this.code = code;
        this.message = message;
        this.data = null;
    }

    public BaseResponse(ICodeMSG codeEnum, T data) {
        this.code = codeEnum.getCode();
        this.message = codeEnum.getMessage();
        this.data = data;
    }

    public BaseResponse(Integer code, String message, T data) {
        this.code = code;
        this.data = data;
        this.message = message;
    }


    /**
     * 静态方法
     *
     * @return
     */
    public static BaseResponse response(Integer code, String message, Object data) {
        return new BaseResponse(code, message, data);
    }

    public static BaseResponse response(Integer code, String message) {
        return new BaseResponse(code, message);
    }

    public static BaseResponse response(ICodeMSG codeEnum) {
        return new BaseResponse(codeEnum);
    }

    public static <R> BaseResponse<R> onOk(R data) {
        return new  BaseResponse<>(ResponseCodeEnum.SUCCESS, data);
    }

    public static <R> BaseResponse<R> onOk() {
        return new BaseResponse<>(ResponseCodeEnum.SUCCESS, null);
    }

    public Integer getCode() {
        return this.code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

}
